/**
 * fileName: AbstractBaseDAO.java
 *     Date: 2012-4-28
 **/
package com.stranger.common.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.stranger.common.util.Pager;

import com.sun.jmx.snmp.ThreadContext;

/**
 * @作者 stranger.lin
 *
 * @时间 2012-4-28
 */
public abstract class AbstractBaseDAO  extends SqlMapClientDaoSupport {

	/* (non-Javadoc)
	 * @see com.stranger.common.dao.impl.IBaseDAO#insert(java.lang.Object)
	 */
	protected Object insert(String nameSpace,Object object) throws SQLException{
		
		return getSqlMapClientTemplate().insert(nameSpace, object);
	}
	
	/* (non-Javadoc)
	 * @see com.stranger.common.dao.impl.IBaseDAO#deleteByPK(java.lang.String)
	 */
	protected int deleteByPK(String nameSpace, String PK) throws SQLException{
		return getSqlMapClientTemplate().delete(nameSpace, PK);
	}
	
	/* (non-Javadoc)
	 * @see com.stranger.common.dao.impl.IBaseDAO#update(java.lang.Object)
	 */
	protected void update(String nameSpace,Object object) throws SQLException{
		
		getSqlMapClientTemplate().update(nameSpace, object);
	}
	/* (non-Javadoc)
	 * @see com.stranger.common.dao.impl.IBaseDAO#load(java.lang.String)
	 */
	protected Object load(String nameSpace,String Pk) throws SQLException{
		return getSqlMapClientTemplate().queryForObject(nameSpace, Pk);
	}
	
	/* (non-Javadoc)
	 * @see com.stranger.common.dao.impl.IBaseDAO#findList(java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	protected List<?> findListByAnyWhere(String nameSpace,Object object) throws SQLException{
		  Pager pager=(Pager)ThreadContext.get("pager");

		List<?> list= getSqlMapClientTemplate().queryForList(
				nameSpace, object,pager.getCurrentRecord(),pager.getPageSize());
		
		List allData= getSqlMapClientTemplate().queryForList(nameSpace, object);
		
		pager.setRows(list);
		pager.setRecords(allData.size());
		
		return list;
	}
}
